
package com.be.beadmin.quality.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.dto.input.ProductSelfCheckInforDto;
import com.be.beadmin.quality.dto.output.ProductSelfCheckInforEx;
import com.be.beadmin.quality.entity.ProductSelfCheckInfor;
import com.be.beadmin.quality.entity.ProductSelfCheckInfor_;
import com.be.beadmin.quality.mapper.ProductSelfCheckInforMapper;
import com.be.beadmin.quality.mapper.ProductSelfCheckInforMapperEx;
import com.be.beadmin.quality.repository.ProductSelfCheckInforRepository;
import com.be.beadmin.quality.service.ProductSelfCheckInforService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-03-19
**/
@Service
@RequiredArgsConstructor
public class ProductSelfCheckInforServiceImpl implements ProductSelfCheckInforService {

    @Autowired
    private  ProductSelfCheckInforRepository productSelfCheckInforRepository;
    @Autowired
    private  ProductSelfCheckInforMapper productSelfCheckInforMapper;
    @Autowired
    private  ProductSelfCheckInforMapperEx productSelfCheckInforExMapper;

    @Override
    public Page<ProductSelfCheckInforEx> getProductSelfCheckInforByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ProductSelfCheckInfor> page = productSelfCheckInforRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ProductSelfCheckInfor_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ProductSelfCheckInfor_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ProductSelfCheckInforEx> page1 = page.map(productSelfCheckInforExMapper::toEntity);
            return page1;

    }

    @Override
    public void addProductSelfCheckInfor(ProductSelfCheckInforDto productSelfCheckInforDto) {
            ProductSelfCheckInfor result = productSelfCheckInforRepository.findProductSelfCheckInforByIdAndDelete(productSelfCheckInforDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("ProductSelfCheckInfor不存在,请重新查询");
            }
            ProductSelfCheckInfor productSelfCheckInfor = productSelfCheckInforMapper.toEntity(productSelfCheckInforDto);
            productSelfCheckInforRepository.save(productSelfCheckInfor);
    }

    @Override
    public void updateProductSelfCheckInfor(ProductSelfCheckInforDto productSelfCheckInforDto) {
            ProductSelfCheckInfor productSelfCheckInfor = productSelfCheckInforRepository.findProductSelfCheckInforById(productSelfCheckInforDto.getId());
            productSelfCheckInfor = productSelfCheckInforMapper.toEntity(productSelfCheckInforDto);
            productSelfCheckInforRepository.save(productSelfCheckInfor);
    }


    @Override
    public void delProductSelfCheckInfor(String id) {
        ProductSelfCheckInfor productSelfCheckInfor = productSelfCheckInforRepository.findProductSelfCheckInforById(id);
        productSelfCheckInfor.setDelete(1);
        productSelfCheckInforRepository.save(productSelfCheckInfor);
    }

    @Override
    public void delProductSelfCheckInfors (List<String> ids) { }

    /**
     * 根据工序id获取改工序的自检信息
     *
     * @param processId
     * @return
     */
    @Override
    public ProductSelfCheckInforEx getProductSelfCheckInforByProcessId(String processId) {
        ProductSelfCheckInfor productSelfCheckInforByProcessIdAndDelete = productSelfCheckInforRepository.findProductSelfCheckInforByProcessIdAndDelete(processId, 0);

        return null;
    }
}